<template>
  <view>
    <u-navbar></u-navbar>
    <view class="content">
      <u-search shape="round" v-model="searchValue" @search="init('搜索')" @custom="init('搜索')"></u-search>
      <Loading :loading-status="loadingStatus"></Loading>
      <view v-if="!loadingStatus">
        <no-data v-if="objects.length==0"></no-data>

        <view v-else>

        <u-card     :head-border-bottom="false" :foot-border-top="false" v-for="object in objects"  @click="back(object)" >
          <view slot="head" style="padding: 0px">
                        <u-row gutter="16" v-if="headShowObjects.length==2">
                          <u-col span="6">
                            <span>{{ object[headShowObjects[0].name] }}</span>
                          </u-col>
                          <u-col span="6">
                            <view style="float: right">
                              <u-tag style="margin-left: 6px;font-size: 10px"  :text="object[headShowObjects[1].name]" size="small" type="warning"/>
                            </view>
                          </u-col>
                        </u-row>
          </view>

          <view slot="body" style="padding: 0px">
            <u-row v-for="(showObject,index) in showObjects" :key="index">
              <u-col span="12">
                <text class="label">{{ showObject.label!=''||showObject.label!=undefined?showObject.label+':':'' }}</text>
                <text class="value">{{ global_dataformat(object[showObject.name]) }}</text>
              </u-col>
            </u-row>
          </view>

          <view slot="foot" style="padding: 0px">

          </view>
        </u-card>
          <u-loadmore v-if="this.system != '1'&&loadingMoreShow" :status="status" />
        </view>
      </view>
      <!-- 正文内容 -->
    </view>
  </view>
</template>

<script>
import {requestInfo} from "../../api/public";
import noData from "./noData";
import Loading from "./Loading";
export default {
  name: "EntitySelect",
  components:{
    noData,
    Loading
  },
  data(){
    return{
      searchValue:'',
      searchInfo:'',
      //过滤信息
      queryParams: {currentPage: 1, filters: {}, orderBys: [], pageSize: 10},
      //实体
      entity:'',
      //模块
      module:'',
      //返回对象
      objects:[],
      //是否为系统参数
      system:'',
      //展示信息配置
      showObjects:'name',
      //使用参数的对象，当一个表单有多个相同模块的实体是区分
      paramType:'',
      //搜索的参数名
      searchParam:'Eid',
      loadingStatus:false,
      status:'loadmore',
      loadingMoreShow:true,

      // 头部展示
      headShowObjects:[],
      // totalCount:100,
    }
  },
  onLoad(option){
    const s = decodeURIComponent(option.entity);
    let searchInfo=JSON.parse(s)
    this.module=searchInfo.module
    this.entity=searchInfo.entity
    this.queryFilters=searchInfo.queryFilters
    this.system=searchInfo.system==undefined?'':searchInfo.system
    this.showObjects=searchInfo.showObjects==undefined?{label:'',name:'name'}:searchInfo.showObjects
    this.paramType=searchInfo.paramType==undefined?'':searchInfo.paramType
    this.queryParams.filters=searchInfo.queryFilters==undefined?{}:searchInfo.queryFilters
    this.headShowObjects=searchInfo.headShowObjects==undefined?[]:searchInfo.headShowObjects
    this.searchParam=searchInfo.searchParam==undefined?'Eid':searchInfo.searchParam
    if (uni.getStorageSync('userInfo').userType==='client'){
      this.queryParams.filters.userId=uni.getStorageSync('userInfo').eid
    }
    this.init()

  },
  methods:{
    init(type) {
      this.queryParams.filters[this.searchParam]='*'+this.searchValue+'*'

      if(type=='loadingMore') {
        this.status = 'loading'
        // console.log("loading")
      }else{
        this.loadingStatus=true
        this.queryParams.currentPage=1
      }



      if (this.system && this.system == '1') {
        // let pageParam={
        //   current:this.queryParams.currentPage,
        //   size:this.queryParams.pageSize
        // }
        requestInfo({
          url: `/${this.module}/${this.entity}`,
          method: 'Get',
        }).then(res => {
          // if(type=='loadingMore') {
          //   if(res.data.data.length==0){
          //     this.status='nomore'
          //     return
          //   }
          //   this.status='loadmore'
          //   this.objects=this.objects.concat(res.data.data())
          // }else{
          this.objects = res.data.data;
          this.loadingStatus=false
          // }

        }).catch(error => {
          console.log(error);
        })
      } else {
        requestInfo({
          url: `/erp/${this.module}/${this.entity}/find${this.entity}s`,
          method: 'POST',
          data: this.queryParams
        }).then(res => {
          // this.totalCount=res.data.totalCount;
          if(type=='loadingMore') {
            if(res.data.datas.length==0){
              this.status='nomore'
              return
            }
            this.status='loadmore'
            this.objects=this.objects.concat(res.data.datas)
          }else{
            this.objects = res.data.datas;
            if(this.objects.length<this.queryParams.pageSize){
              this.loadingMoreShow=false
              this.status='nomore'
            }else{
              this.loadingMoreShow=true
              this.status='loadmore'
            }
            this.loadingStatus=false
          }
          // console.log(res.data)
          //单字段过滤器
          // if (this.fixedFilterOpt !== null) {
          //   this.tempObj=this.objects;
          //   //先清空数据
          //   this.objects=[];
          //   let tempArr = [];
          //   this.tempObj.forEach((item) => {
          //     if (item[Object.keys(this.fixedFilterOpt)[0]] == Object.values(this.fixedFilterOpt)[0]) {
          //       tempArr.push(item);
          //     }
          //   })
          //   this.objects = tempArr;
          // }
        }).catch(error => {
          console.log(error);
        })
      }
    },
    back(object){
      uni.$emit('updateData',
          {
            object:object,
            type:this.entity,
            paramType:this.paramType
          })
      uni.navigateBack({
        delta: 1
      })

    },
    //懒加载
    onReachBottom() {
      // console.log(123)
      this.queryParams.currentPage+=1
      if(this.status=="nomore"){
        // this.status='nomore'
        return
      }
      this.init("loadingMore")
    },
  }
}
</script>

<style >
.u-card__head{
  padding: 0px!important;
}
.u-card__body{
  padding: 0px!important;
}
.u-row{
  padding: 2px!important;
}
.u-card__foot{
  padding: 0px!important;
}
.u-card{
  padding: 10px;
  border-radius: 20px!important;
  font-size: 14px;
}
.u-tag{
  padding: 2px 5px
}
.label{
  color: #a0a1aa;
  font-size: 24rpx;
  display:inline-block;text-align:justify;text-align-last:justify;
  margin-right: 4px;
}
.value{
  font-size: 26rpx;
}
</style>
